package com.projectx.action.chat;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.projectx.R;
import com.projectx.action.chat.DevoteActivity.DevoteActivity;
import com.projectx.action.chat.GroupAnnocement.GroupDialogActivity;
import com.projectx.action.draw.DrawBoardActivity;
import com.projectx.adpter.ChatDataAdapter;
import com.projectx.entity.ChatData;
import com.projectx.util.ActivityManage;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class GroupChatActivity extends AppCompatActivity {
    private String username;
    private String targetName;
    private RecyclerView recyclerView;
    private Button setting;
    private Button fileShare;
    private Button drawPage;
    private Button back;
    private Button insetImage;
    private Button send;
    private Button groupDialog;//公告按钮
    private Button devote;//投票按钮
    private TextView groupName;
    private EditText editText;
    private ImageView thumb;
    private ImageView deleteImage;
    private TextView waitSendText;
    private List<ChatData> chatDataSet = new ArrayList<>();
    private Uri imageUri;
    private PopupWindow popupWindow;
    private TextView hintText;
    private LinearLayout hintTextContainer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        targetName = intent.getStringExtra("targetName");
        username = intent.getStringExtra("accountName");
        ActivityManage.addActivity("GroupChatActivity" + targetName,this);
        setContentView(R.layout.activity_group_chat);

        try {
            getSupportActionBar().hide(); //去除标题栏
        } catch (NullPointerException e){
            Log.d("KMH","没有状态栏标题");
        }

        groupName = findViewById(R.id.groupName);
        setting = findViewById(R.id.setting_chat);
        fileShare = findViewById(R.id.file_share);
        drawPage = findViewById(R.id.draw);
        back = findViewById(R.id.Button_back);
        insetImage = findViewById(R.id.insert_imagine);
        recyclerView = findViewById(R.id.recycle_view_group_chat);
        send = findViewById(R.id.chatSend);
        editText = findViewById(R.id.chatEditText);
        devote = findViewById(R.id.devote);
        groupDialog = findViewById(R.id.dialog);
        thumb = findViewById(R.id.thumb);
        waitSendText = findViewById(R.id.wait_sent_text);
        waitSendText.setVisibility(View.INVISIBLE);
        deleteImage = findViewById(R.id.delete_thumb);
        deleteImage.setVisibility(View.INVISIBLE);
        popupWindow = new PopupWindow(this);
        popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_shape_hint_corner));


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setStackFromEnd(true);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(new ChatDataAdapter(this, chatDataSet,username));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        recyclerView.setItemViewCacheSize(100);

        deleteImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageUri = null;
                thumb.setImageURI(null);
                waitSendText.setVisibility(View.INVISIBLE);
                deleteImage.setVisibility(View.INVISIBLE);
            }
        });

        groupName.setText(targetName);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GroupChatActivity.this, ChatSettingActivity.class);
                startActivity(intent);
            }
        });

        fileShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("*/*");
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                startActivityForResult(intent,1);
            }
        });
        fileShare.setOnLongClickListener(new View.OnLongClickListener() {//长按显示提示
            @Override
            public boolean onLongClick(View v) {
                View layout = LayoutInflater.from(GroupChatActivity.this).inflate(R.layout.item_one_hint_text,null);
                hintTextContainer = layout.findViewById(R.id.hint_text_container);
                hintText = layout.findViewById(R.id.hint_text);
                hintText.setText("文件发送");
                popupWindow.setContentView(hintTextContainer);
                popupWindow.showAsDropDown(fileShare,0,-300);
                hintText.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                    }
                });
                return true;
            }
        });

        insetImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                startActivityForResult(intent,2);
            }
        });

        insetImage.setOnLongClickListener(new View.OnLongClickListener() {//长按显示提示
            @Override
            public boolean onLongClick(View v) {
                View layout = LayoutInflater.from(GroupChatActivity.this).inflate(R.layout.item_one_hint_text,null);
                hintTextContainer = layout.findViewById(R.id.hint_text_container);
                hintText = layout.findViewById(R.id.hint_text);
                hintText.setText("分享图片");
                popupWindow.setContentView(hintTextContainer);
                popupWindow.showAsDropDown(insetImage,-56,-300);
                hintText.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                    }
                });
                return true;
            }
        });

        drawPage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//首先关闭键盘，防止意外
                InputMethodManager imm =  (InputMethodManager)GroupChatActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
                if(imm != null) {
                    imm.hideSoftInputFromWindow(GroupChatActivity.this.getWindow().getDecorView().getWindowToken(), 0);
                }
                Intent intent = new Intent(GroupChatActivity.this, DrawBoardActivity.class);
                startActivity(intent);
            }
        });
        drawPage.setOnLongClickListener(new View.OnLongClickListener() {//长按显示提示
            @Override
            public boolean onLongClick(View v) {
                View layout = LayoutInflater.from(GroupChatActivity.this).inflate(R.layout.item_one_hint_text,null);
                hintTextContainer = layout.findViewById(R.id.hint_text_container);
                hintText = layout.findViewById(R.id.hint_text);
                hintText.setText("画图");
                popupWindow.setContentView(hintTextContainer);
                popupWindow.showAsDropDown(drawPage,-36,-300);
                hintText.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                    }
                });
                return true;
            }
        });

        devote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(GroupChatActivity.this, DevoteActivity.class);
                startActivity(intent);
            }
        });

        devote.setOnLongClickListener(new View.OnLongClickListener() {//长按显示提示
            @Override
            public boolean onLongClick(View v) {
                View layout = LayoutInflater.from(GroupChatActivity.this).inflate(R.layout.item_one_hint_text,null);
                hintTextContainer = layout.findViewById(R.id.hint_text_container);
                hintText = layout.findViewById(R.id.hint_text);
                hintText.setText("投票");
                popupWindow.setContentView(hintTextContainer);
                popupWindow.showAsDropDown(devote,-36,-300);
                hintText.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                    }
                });
                return true;
            }
        });

        groupDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(GroupChatActivity.this, GroupDialogActivity.class);
                startActivity(intent);
            }
        });

        groupDialog.setOnLongClickListener(new View.OnLongClickListener() {//长按显示提示
            @Override
            public boolean onLongClick(View v) {
                View layout = LayoutInflater.from(GroupChatActivity.this).inflate(R.layout.item_one_hint_text,null);
                hintTextContainer = layout.findViewById(R.id.hint_text_container);
                hintText = layout.findViewById(R.id.hint_text);
                hintText.setText("群公告");
                popupWindow.setContentView(hintTextContainer);
                popupWindow.showAsDropDown(groupDialog,-48,-300);
                hintText.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                    }
                });
                return true;
            }
        });

        send.setOnClickListener(new View.OnClickListener() { //发送按钮
            @Override
            public void onClick(View v) {
                ChatData chatData = new ChatData();
                Date time = new Date();
                chatData.setTime(time);
                chatData.setSendUser(username);
                chatData.setBelongToUser(username);
                chatData.setRecord(editText.getText().toString());
                if (imageUri != null){
                    chatData.setImageUri(imageUri);
                    imageUri = null;
                }
                chatDataSet.add(chatData);
                editText.setText("");
                thumb.setImageURI(null);
                waitSendText.setVisibility(View.INVISIBLE);
                deleteImage.setVisibility(View.INVISIBLE);
                if (recyclerView.getAdapter() != null){
                    recyclerView.getAdapter().notifyItemInserted(chatDataSet.size() - 1);//更新一下的数据
                    recyclerView.scrollToPosition(chatDataSet.size() - 1);
                }
            }
        });

        editText.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                recyclerView.scrollToPosition(chatDataSet.size() - 1);
            }
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1){
            if (data != null){
                Uri file = data.getData();
                Log.d("kmh", "onActivityResult: " + file.toString());
            }
        } else if(requestCode == 2){
            if (data != null){
                imageUri = data.getData();
                waitSendText.setVisibility(View.VISIBLE);
                thumb.setImageURI(imageUri);
                deleteImage.setVisibility(View.VISIBLE);
                Log.d("kmh", "onActivityResult: "+ imageUri.toString());
            }
        } else {
            imageUri = null;
            thumb.setImageURI(null);
            waitSendText.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void onBackPressed() {
        ActivityManage.removeActivity("SingleChatActivity" + targetName);
        super.onBackPressed();
    }
}
